package com.dapeis.web.admin.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ag.core.base.Pager;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Collocation;
import com.dapeis.domain.User;
import com.dapeis.service.UserService;

/**
 * 后台用户管理 Title:AdminUserController.java AdminUserController Description:
 * 
 * @author yuanyong
 * @date 2017年4月19日 下午2:29:57 Email: hzyuanyong@126.com
 *
 */

@Controller
@RequestMapping(value = "/admin/member")
public class AdminMemberController extends AdminController {

    @Resource
    UserService userService;

    @RequestMapping("/list")
    public String userList(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	if (null == pager) {
	    pager = new Pager();
	}
	userService.pager(pager);
	model.put("pager", pager);
	return doPackageURI("user/list");
    }
    
    
    
    @RequestMapping("/{userId}/checkState")
    public String checkState(@PathVariable Integer userId,
	    Map<Object, Object> model, RedirectAttributes attr,
	    HttpServletRequest request) {
	User user = userService
		.selectByPrimaryKey(userId);
	if (null == user) {
	    throw new RuntimeException("找不到搭配师 Id： " + user);
	}
	user
		.setState(user.getState() == StylistConstants.State.NORMAL ? StylistConstants.State.DISABLE
			: StylistConstants.State.NORMAL);
	userService.updateByPrimaryKey(user);
	this.setInfoMsg("操作成功!", attr);
	String redirect = request.getParameter("redirect");
	if (StringUtils.isNotBlank(redirect)) {
	    return this.redirect(redirect);
	} else {
	    return this.redirect("/admin/member/list");
	}
    }
}



